var QUESTION_ID;

var _spex = _$("question_detail")._$id();

var _fouc = "onfocus",
    _blur = "onblur",
    changeEvent = 'onkeydown' && 'onkeyup',

    changesEvent = function () {
        if (this.getAttribute("id") == 'question_detail') {
            if (this.scrollHeight >= 500) {

                this.style.height = '500px';
                this.style.overflowY = 'auto';
                return false;
            }


            this.style.height = '0px';
            if (this.scrollHeight >= 320) {

                this.style.height = '0px';
                this.style.height = this.scrollHeight + 50 + 'px';

            } else {

                this.style.height = '320px';
            }

        } else {

            if (this.scrollHeight >= 500) {

                this.style.height = '500px';
                this.style.overflowY = 'auto';
                return false;
            }
            this.style.height = '0px';
            this.style.height = this.scrollHeight + 60 + 'px';
        }

    },
    elementEventFocus = function () {

        if (this.value == '') {
            if (this.getAttribute("id") == 'question_detail') {
                this.style.height = '320px';

            } else {
                this.style.height = '80px';
            }
        }


    },

    elementEventblur = function () {

        if (this.value == '') {
            if (this.getAttribute("id") == 'question_detail') {
                this.style.height = '320px';
            } else {
                this.style.height = '80px';
            }

        }
    };

if (_spex) {

    _spex[changeEvent] = changesEvent; //绑定change事件
    _spex[_fouc] = elementEventFocus;
    _spex[_blur] = elementEventblur;
}


function hide(o, j, c, p, x) {

    var addtxt = d.getElementById("addtxt");

    var x = d.getElementById(o);
    var xjx = d.getElementById(j);
    x.className = c;
    xjx.className = p;
}

function addElementVALUE() {

    var inps = d.getElementById('editorInput').getElementsByTagName("input")[0],
        classname = null;
    var addtxt = d.getElementById('addtxt');
    var s_span = addtxt.getElementsByTagName("span");

    if (inps.className == 'i_add') {

        classname = inps;
    }

    var inpsValue = classname.value; //.replace(/\s/g,'');

    if (inpsValue == '') {
        return false;
    }

    for (var xi = 0, len = s_span.length; xi < len; xi++) {

        var s = s_span[xi];
        if (s.getAttribute("val") == inpsValue) {

            return false;
        }
    }
    var a = '<a href="' + G_BASE_URL + '/topic/' + inps.value + '">' + inpsValue + '</a>';
    var s = d.createElement("span");
    s.setAttribute("val", inpsValue);
    s.innerHTML = a + '<em onclick="removeChilds(this);">x</em><input type="hidden" name="topics[]" value="' + inpsValue + '" />';
    d.getElementById('addtxt').appendChild(s);
    d.getElementById('addtxt').className = 'add_C'
    inps.value = '';

}



var removeChilds = function (el) {
        var parent = el.parentNode.parentNode;
        parent.removeChild(el.parentNode);
        var s = parent.getElementsByTagName('span');
        if (s.length <= 0) {
            parent.className = 'add_C hide';
        } else {
            parent.className = 'add_C';
        }
    }
var t;
var m = {

    mx: function () {

        var doc = document,
            mxId = document.getElementById("Manytopics"),
            ajaxDate = document.getElementById("ajaxDate"),
            take = document.getElementById("take"),
            txt = document.getElementById("txt"),
            addtxt = document.getElementById("addtxt");

        var mxinput = document.getElementById("ajaxValue_txt");
        var mxDiv = mxId.getElementsByTagName("div")[0];
        var mxSpan = mxDiv.getElementsByTagName("span");



        var el = this;

        return {

            add: function (a, val) {

                var s = doc.createElement("span");
                s.innerHTML = a + '<em onclick="removeChilds(this);">x</em><input type="hidden" name="topics[]" value="' + val + '" />';
                s.setAttribute('val', val);
                return s;

            },

            app: function (x) {

                var objValue = x.getAttribute("date-list");

                var v = addtxt.getElementsByTagName("span");

                for (var i = 0, len = v.length; i < len; i++) {
                    if (v[i].getAttribute("date-list") == objValue) {
                        mxinput.classNam = 'i_add inErr';
                        mxinput.value = '';
                        doc.getElementById("err").innerHTML = '请勿重复添加话题！';
                        doc.getElementById("err").className = 'cr';
                        return false;
                    }
                }

                var el = this;

                var a = '<a href="javascript:;">' + objValue + '</a>';
                var s = el.add(a, objValue);
                mxDiv.appendChild(s);
                ajaxDate.className = 'hide';
                mxinput.value = '';
                mxinput.focus();
                doc.getElementById('addtxt').className = 'add_C'


            },

            ev: function () {
                var elx = this;

                mxinput['onkyedown' && 'onkeyup'] = function () {

                    mxinput.classNam = 'i_add';
                    doc.getElementById("err").className = 'cr hide';
                    if (this.value.replace(/\s/g, '') == '') {
                        ajaxDate.className = 'hide';
                        return false;
                    }
                    var xmlvalue, XHR;
                    var objValue = this.value;
                    var strValue = objValue.toString();
                    var stv = strValue.replace(/\s/g, '');
                    take.setAttribute("date-list", stv);
                    take.innerHTML = stv;
                    var url = G_BASE_URL + '/search/ajax/search/type-topic_add__q-' + stv + '__limit-100';


                    XHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("microsoft.XMLHTTP");
                    XHR.open("GET", url, true);
                    XHR.onreadystatechange = function () {

                        if (XHR.readyState == 4 && XHR.status == 200) {

                            ajaxDate.className = '';
                            if (XHR.responseText.length > 2) {


                                txt.getAttribute('class') == 'hide' ? txt.className = '' : '';
                                xmlvalue = XHR.responseText;
                                var x = new Function('return' + xmlvalue)();

                                for (var i = -1, len = x.length; ++i < len;) {

                                    if (i !== 0) {

                                        txt.getElementsByTagName("span")[0].className = ' hide';
                                        var elementsAjax = doc.createElement("div");
                                        elementsAjax.onclick = function () {
                                            add(this.id);
                                        };
                                        elementsAjax.id = x[i].sno;
                                        elementsAjax.setAttribute("date-list", x[i].name);
                                        elementsAjax.innerHTML = x[i].name + '<p>关注：<span>' + (x[i].detail.focus_count == false ? 0 : x[i].detail.focus_count) + '</span> 人</p>';
                                        txt.appendChild(elementsAjax);


                                    } else if (i == 0) {

                                        if (x[i].exist == 0) {
                                            take.parentNode.className = 'add';
                                            txt.getElementsByTagName("span")[0].className = ' hide';
                                        } else if (x[i].exist == 1) {
                                            take.parentNode.className = 'add hide';
                                        }
                                    }

                                }

                            } else {
                                take.parentNode.className = 'add';
                                txt.className = 'hide';
                            } //end responseText

                        } else {

                            txt.innerHTML = "<span style='color:#999;'><img src='" + G_STATIC_URL + "/common/load.gif'/> 正在加载..</span>";

                        }

                    };

                    XHR.send(null);


                }

                doc.onclick = function () {
                    ajaxDate.className = 'hide';
                }

            },

            info: function () {
                var seif = this;
                mxinput['onfocus'] = function () {
                    seif.ev()
                };

            }


        }


    },

    remove: function () {

        var o = this;
        o.parentNode.parentNode.removeChild(o.parentNode);

    }
}


function form_submit(obj) {
	var question_content = document.getElementById(obj);

	if (question_content.value == '请输入您要发起的问题...') {
	    new DialogBox_show('250', '请输入问题标题', '提示信息', '确定');
	    return false;
	} else {
	    ajax_post($('#question_form'));
	}
}
var t =null;
$(document).ready(function () {
    QUESTION_ID = document.getElementById('question_id').value;

    if (!window.location.hash) {
        window.location.hash = 'question';
    }

    if (document.getElementById('captcha')) {
        document.getElementById('captcha').click();
    }

    init_fileuploader('file_uploader_question', G_BASE_URL + '/publish/ajax/attach_upload/id-question__attach_access_key-' + ATTACH_ACCESS_KEY);

    if (QUESTION_ID && G_UPLOAD_ENABLE == 'Y') {
        if ($("#file_uploader_question ._ajax_upload-list").attr('class')) {
            $.post(G_BASE_URL + '/publish/ajax/question_attach_edit_list/', "question_id=" + QUESTION_ID, function (data) {
                if (data['err']) {
                    //alert(data['err']);

                    return false;
                } else {
                    $.each(data['rsm']['attachs'], function (i, v) {
                        _ajax_uploader_append_file("#file_uploader_question ._ajax_upload-list", v);
                    });
                }
            }, 'json');
        }
    }

    if (window.location.hash && $('#' + window.location.hash.replace('#', ''))) {
        $('#' + window.location.hash.replace('#', '')).click();
    }

    switch (window.location.hash) {
    case '#question':
        $('#publish_' + window.location.hash.replace('#', '')).click();
        break;
    }

 t = new m.mx();
    t.info();

   
});

function add(x) {
	var o = 'string' == typeof x ? document.getElementById(x) : x;
	t.app(o);
}